using Microsoft.VisualBasic;
using Microsoft.VisualBasic.CompilerServices;
using SJVars;
using System;
using System.Collections;
using yW3Lib;

namespace yW5Lib
{
	public class clsImportYW3
	{
		private clsProjectIO cProjectIO;

		public clsImportYW3()
		{
			cProjectIO = new clsProjectIO();
		}

		public bool ImportYW3(ref clsProject cProject, string YW3File = "")
		{
			//Discarded unreachable code: IL_0369, IL_0380, IL_038b, IL_039b, IL_049d, IL_049f, IL_04b3, IL_04d5
			int num = default(int);
			bool result = false;
			int num4 = default(int);
			try
			{
				ProjectData.ClearProjectError();
				num = 2;
				int num2 = 2;
				result = true;
				num2 = 3;
				clsYW3Project clsYW3Project = new clsYW3Project();
				num2 = 4;
				if (Operators.CompareString(YW3File, "", TextCompare: false) == 0)
				{
					num2 = 5;
					YW3File = clsShared.cFileSel.GetFile(clsShared.cSpecialFolders.MyDocsFolder, "*.YW3");
				}
				num2 = 6;
				if (Operators.CompareString(YW3File, "", TextCompare: false) == 0)
				{
					num2 = 7;
					result = false;
				}
				else
				{
					num2 = 9;
					if (!clsYW3Project.ImportYW3(YW3File))
					{
						num2 = 10;
						Interaction.MsgBox("There was an error loading your yW3 project");
						num2 = 11;
						result = false;
					}
					else
					{
						num2 = 14;
						if (cProject.CreateEmptyProject(V6Project: false))
						{
							num2 = 15;
							cProject.Title = clsYW3Project.Title;
							num2 = 16;
							cProject.AuthorName = clsYW3Project.AuthorName;
							num2 = 17;
							int num3 = 1;
							do
							{
								num2 = 18;
								cProject.set_DeadlineDate(num3, clsYW3Project.get_DeadlineDate(num3));
								num2 = 19;
								cProject.set_DeadlineStartDate(num3, clsYW3Project.get_DeadlineStartDate(num3));
								num2 = 20;
								num3++;
							}
							while (num3 <= 5);
							num2 = 21;
							cProject.WordCountStart = clsYW3Project.WordCountStart;
							num2 = 22;
							cProject.WordTarget = clsYW3Project.WordTarget;
							num2 = 23;
							cProject.WordTargetEndDate = clsYW3Project.WordTargetEndDate;
							num2 = 24;
							cProject.WordTargetStartDate = clsYW3Project.WordTargetStartDate;
							num2 = 25;
							IEnumerator enumerator = clsYW3Project.Chars.GetEnumerator();
							while (enumerator.MoveNext())
							{
								clsYW3Character YW3Char = (clsYW3Character)enumerator.Current;
								num2 = 26;
								clsCharacter cCh = new clsCharacter();
								num2 = 27;
								TransferChar(ref YW3Char, ref cCh);
								num2 = 28;
								cProject.AddChar(ref cCh);
								num2 = 29;
							}
							num2 = 30;
							if (enumerator is IDisposable)
							{
								(enumerator as IDisposable).Dispose();
							}
							num2 = 31;
							IEnumerator enumerator2 = clsYW3Project.Items.GetEnumerator();
							while (enumerator2.MoveNext())
							{
								clsYW3Item YW3Item = (clsYW3Item)enumerator2.Current;
								num2 = 32;
								clsItem cItem = new clsItem();
								num2 = 33;
								TransferItem(ref YW3Item, ref cItem);
								num2 = 34;
								cProject.AddItem(ref cItem);
								num2 = 35;
							}
							num2 = 36;
							if (enumerator2 is IDisposable)
							{
								(enumerator2 as IDisposable).Dispose();
							}
							num2 = 37;
							IEnumerator enumerator3 = clsYW3Project.Locations.GetEnumerator();
							while (enumerator3.MoveNext())
							{
								clsYW3Location YW3Location = (clsYW3Location)enumerator3.Current;
								num2 = 38;
								clsLocation cLocation = new clsLocation();
								num2 = 39;
								TransferLocation(ref YW3Location, ref cLocation);
								num2 = 40;
								cProject.AddLocation(ref cLocation);
								num2 = 41;
							}
							num2 = 42;
							if (enumerator3 is IDisposable)
							{
								(enumerator3 as IDisposable).Dispose();
							}
							num2 = 43;
							IEnumerator enumerator4 = clsYW3Project.Chapters.GetEnumerator();
							while (enumerator4.MoveNext())
							{
								clsYW3Chapter YW3Chapter = (clsYW3Chapter)enumerator4.Current;
								num2 = 44;
								clsChapter cChapter = new clsChapter();
								num2 = 45;
								TransferChapter(ref YW3Chapter, ref cChapter);
								num2 = 46;
								cProject.AddChapter(ref cChapter);
								num2 = 47;
							}
							num2 = 48;
							if (enumerator4 is IDisposable)
							{
								(enumerator4 as IDisposable).Dispose();
							}
							num2 = 49;
							IEnumerator enumerator5 = clsYW3Project.Scenes.GetEnumerator();
							while (enumerator5.MoveNext())
							{
								clsYW3Scene YW3Scene = (clsYW3Scene)enumerator5.Current;
								num2 = 50;
								clsScene Scene = new clsScene();
								num2 = 51;
								Scene.V6 = cProject.V6File;
								num2 = 52;
								TransferScene(ref YW3Scene, ref Scene);
								num2 = 53;
								cProject.AddSceneToProjectAndChapter(ref Scene, Scene.BelongsToChID);
								num2 = 54;
							}
							num2 = 55;
							if (enumerator5 is IDisposable)
							{
								(enumerator5 as IDisposable).Dispose();
							}
						}
						num2 = 56;
						if (cProject.CriticalSave())
						{
							num2 = 57;
							result = true;
						}
					}
				}
			}
			catch (Exception obj) when ((obj is Exception && num != 0) & (num4 == 0))
			{
				ProjectData.SetProjectError((Exception)obj);
				/*Error near IL_04d3: Could not find block for branch target IL_049f*/;
			}
			if (num4 != 0)
			{
				ProjectData.ClearProjectError();
			}
			return result;
		}

		private void TransferChar(ref clsYW3Character YW3Char, ref clsCharacter cCh)
		{
			//Discarded unreachable code: IL_00df, IL_00f6, IL_0101, IL_0111, IL_0167, IL_0169, IL_017d, IL_019f
			int num = default(int);
			int num3 = default(int);
			try
			{
				ProjectData.ClearProjectError();
				num = 2;
				int num2 = 2;
				cCh.AKA = YW3Char.AlternateNames;
				num2 = 3;
				cCh.Bio = YW3Char.Bio;
				num2 = 4;
				cCh.DescText = YW3Char.DescText;
				num2 = 5;
				cCh.FullName = YW3Char.FullName;
				num2 = 6;
				cCh.Goals = YW3Char.Goals;
				num2 = 7;
				cCh.ID = YW3Char.ID;
				num2 = 8;
				cCh.ImageFile = YW3Char.ImageFile;
				num2 = 9;
				cCh.Major = YW3Char.Major;
				num2 = 10;
				cCh.Notes = YW3Char.Notes;
				num2 = 11;
				cCh.SortOrder = YW3Char.SortOrder;
				num2 = 12;
				cCh.Tags = YW3Char.Tags;
				num2 = 13;
				cCh.Title = YW3Char.Title;
				num2 = 14;
				cCh.Unused = YW3Char.Unused;
			}
			catch (Exception obj) when ((obj is Exception && num != 0) & (num3 == 0))
			{
				ProjectData.SetProjectError((Exception)obj);
				/*Error near IL_019d: Could not find block for branch target IL_0169*/;
			}
			if (num3 != 0)
			{
				ProjectData.ClearProjectError();
			}
		}

		private void TransferItem(ref clsYW3Item YW3Item, ref clsItem cItem)
		{
			//Discarded unreachable code: IL_0079, IL_0090, IL_009b, IL_00ab, IL_00e9, IL_00eb, IL_00ff, IL_0121
			int num = default(int);
			int num3 = default(int);
			try
			{
				ProjectData.ClearProjectError();
				num = 2;
				int num2 = 2;
				cItem.DescText = YW3Item.DescText;
				num2 = 3;
				cItem.ID = YW3Item.ID;
				num2 = 4;
				cItem.ImageFile = YW3Item.ImageFile;
				num2 = 5;
				cItem.SortOrder = YW3Item.SortOrder;
				num2 = 6;
				cItem.Tags = YW3Item.Tags;
				num2 = 7;
				cItem.Title = YW3Item.Title;
				num2 = 8;
				cItem.Unused = YW3Item.Unused;
			}
			catch (Exception obj) when ((obj is Exception && num != 0) & (num3 == 0))
			{
				ProjectData.SetProjectError((Exception)obj);
				/*Error near IL_011f: Could not find block for branch target IL_00eb*/;
			}
			if (num3 != 0)
			{
				ProjectData.ClearProjectError();
			}
		}

		private void TransferLocation(ref clsYW3Location YW3Location, ref clsLocation cLocation)
		{
			//Discarded unreachable code: IL_0079, IL_0090, IL_009b, IL_00ab, IL_00e9, IL_00eb, IL_00ff, IL_0121
			int num = default(int);
			int num3 = default(int);
			try
			{
				ProjectData.ClearProjectError();
				num = 2;
				int num2 = 2;
				cLocation.DescText = YW3Location.DescText;
				num2 = 3;
				cLocation.ID = YW3Location.ID;
				num2 = 4;
				cLocation.ImageFile = YW3Location.ImageFile;
				num2 = 5;
				cLocation.SortOrder = YW3Location.SortOrder;
				num2 = 6;
				cLocation.Tags = YW3Location.Tags;
				num2 = 7;
				cLocation.Title = YW3Location.Title;
				num2 = 8;
				cLocation.Unused = YW3Location.Unused;
			}
			catch (Exception obj) when ((obj is Exception && num != 0) & (num3 == 0))
			{
				ProjectData.SetProjectError((Exception)obj);
				/*Error near IL_011f: Could not find block for branch target IL_00eb*/;
			}
			if (num3 != 0)
			{
				ProjectData.ClearProjectError();
			}
		}

		private void TransferScene(ref clsYW3Scene YW3Scene, ref clsScene Scene)
		{
			//Discarded unreachable code: IL_0234, IL_024b, IL_0256, IL_0266, IL_0310, IL_0312, IL_0326, IL_0348
			int num = default(int);
			int num5 = default(int);
			try
			{
				ProjectData.ClearProjectError();
				num = 2;
				int num2 = 2;
				Scene.BelongsToChID = YW3Scene.BelongsToChID;
				num2 = 3;
				Scene.Conflict = YW3Scene.Conflict;
				num2 = 4;
				Scene.Day_Renamed = YW3Scene.Day_Renamed;
				num2 = 5;
				Scene.DescText = YW3Scene.DescText;
				num2 = 6;
				int num3 = 1;
				do
				{
					num2 = 7;
					((clsSceneBase)Scene).set_Field(num3, YW3Scene.get_Field(num3));
					num2 = 8;
					num3++;
				}
				while (num3 <= 4);
				num2 = 9;
				Scene.Goal = YW3Scene.Goal;
				num2 = 10;
				Scene.Hour_Renamed = YW3Scene.Hour_Renamed;
				num2 = 11;
				Scene.ID = YW3Scene.ID;
				num2 = 12;
				Scene.ImageFile = YW3Scene.ImageFile;
				num2 = 13;
				Scene.LastsDays = YW3Scene.LastsDays;
				num2 = 14;
				Scene.LastsHours = YW3Scene.LastsHours;
				num2 = 15;
				Scene.LastsMinutes = YW3Scene.LastsMinutes;
				num2 = 16;
				Scene.Minute_Renamed = YW3Scene.Minute_Renamed;
				num2 = 17;
				Scene.Notes = YW3Scene.Notes;
				num2 = 18;
				Scene.Outcome = YW3Scene.Outcome;
				num2 = 19;
				Scene.ReactionScene = YW3Scene.ReactionScene;
				num2 = 20;
				Scene.Status = YW3Scene.Status;
				num2 = 21;
				Scene.SubPlot = YW3Scene.SubPlot;
				num2 = 22;
				Scene.Tags = YW3Scene.Tags;
				num2 = 23;
				Scene.TextRTF = YW3Scene.TextRTF;
				num2 = 24;
				Scene.Title = YW3Scene.Title;
				num2 = 25;
				Scene.Unused = YW3Scene.Unused;
				num2 = 26;
				int itemCount = YW3Scene.ItemCount;
				for (int i = 1; i <= itemCount; i++)
				{
					num2 = 27;
					Scene.AddItemByID(YW3Scene.ItemIDByIndex(i));
					num2 = 28;
				}
				num2 = 29;
				int charCount = YW3Scene.CharCount;
				for (int i = 1; i <= charCount; i++)
				{
					num2 = 30;
					Scene.AddCharByID(YW3Scene.CharIDByIndex(i));
					num2 = 31;
				}
				num2 = 32;
				int num4 = YW3Scene.LocationCount();
				for (int i = 1; i <= num4; i++)
				{
					num2 = 33;
					Scene.AddLocationByID(YW3Scene.LocationIDByIndex(i));
					num2 = 34;
				}
				num2 = 35;
				Scene.SetViewpointByID(YW3Scene.ViewPointID);
			}
			catch (Exception obj) when ((obj is Exception && num != 0) & (num5 == 0))
			{
				ProjectData.SetProjectError((Exception)obj);
				/*Error near IL_0346: Could not find block for branch target IL_0312*/;
			}
			if (num5 != 0)
			{
				ProjectData.ClearProjectError();
			}
		}

		private void TransferChapter(ref clsYW3Chapter YW3Chapter, ref clsChapter cChapter)
		{
			cChapter.DescText = YW3Chapter.DescText;
			cChapter.ID = YW3Chapter.ID;
			cChapter.SortOrder = YW3Chapter.SortOrder;
			cChapter.Tags = YW3Chapter.Tags;
			cChapter.Title = YW3Chapter.Title;
			cChapter.Type = ChapterTypes.ChapterNormal;
			cChapter.Unused = YW3Chapter.Unused;
			for (int i = YW3Chapter.SceneCount; i >= 1; i += -1)
			{
				cChapter.AddSceneID(YW3Chapter.SceneIDByIndex(i));
			}
		}
	}
}
